package com.pig4cloud.pig.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pig.admin.api.entity.NewsList;
import com.pig4cloud.pig.admin.api.entity.PasswordList;
import com.pig4cloud.pig.admin.constant.CommonConstant;
import com.pig4cloud.pig.admin.mapper.PasswordListMapper;
import com.pig4cloud.pig.admin.service.NewsListService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.pig4cloud.pig.admin.mapper.NewsListMapper;

import java.util.*;

@Service
@AllArgsConstructor
public class NewsListServiceImpl extends ServiceImpl<NewsListMapper, NewsList> implements NewsListService {

	private final PasswordListMapper passwordListMapper;
	private final NewsListMapper newsListMapper;


	@Override
	public String insertTest(PasswordList passwordList) {
		NewsList newsList = new NewsList();
		//newsList.setId("1");
		newsList.setTitle("111");
		newsList.setSubTitle("222");
		Map<String,String> map1 = new HashMap<>();
		map1.put("num1","1");
		map1.put("num1","2");
		Map<String,String> map2 = new HashMap<>();
		map1.put("num3","3");
		map1.put("num4","4");
		List<Map<String,String>> list = new ArrayList<>();
		list.add(map1);
		list.add(map2);
		//newsList.setImgJson(list);
		try {
			passwordListMapper.insertTest(newsList);
			return "success";
		}catch (Exception e){
			e.printStackTrace();
			return "failed";
		}
	}

	/**
	 * 分页查询
	 * @param page
	 * @param newsList
	 * @return
	 */
	@Override
	public Page getNewsListByPage(Page page, NewsList newsList) {
		// 查询条件
		LambdaQueryWrapper<NewsList> wrapper = new LambdaQueryWrapper<>();
		// 查询
		String category = newsList.getCategory();
		if (ObjectUtils.isNotEmpty(category)) {
			if(category.contains(",")){
				String[] split = category.split(",");
				wrapper.in(NewsList::getCategory,split);
			}else{
				wrapper.eq(NewsList::getCategory, newsList.getCategory());
			}
		}
		// 今天”，“本周”，“本月”，“本季度”，“本年度
		if (ObjectUtils.isNotEmpty(newsList.getDateRange())) {
			Map<String, Date> timeRange = CommonConstant.getTimeRange(newsList.getDateRange());
			wrapper.ge(NewsList::getCreateTime, timeRange.get("start"));
			wrapper.lt(NewsList::getCreateTime, timeRange.get("end"));
		}
		if (ObjectUtils.isNotEmpty(newsList.getTitle())) {
			wrapper.like(NewsList::getTitle, newsList.getTitle());
		}
		wrapper.orderByDesc(NewsList::getCreateTime);
		//wrapper.last("ORDER BY RAND()");
		return newsListMapper.selectPage(page,wrapper);
	}


	@Override
	public Page selectRandom(Page page, NewsList newsList) {
		// 查询
		Map<String,Object> map = new HashMap<>();
		String category = newsList.getCategory();
		if (ObjectUtils.isNotEmpty(category)) {
			if(category.contains(",")){
				String[] split = category.split(",");
				map.put("category",Arrays.asList(split));
			}else{
				map.put("category",Arrays.asList(newsList.getCategory()));
			}
		}
		// 今天”，“本周”，“本月”，“本季度”，“本年度
		if (ObjectUtils.isNotEmpty(newsList.getDateRange())) {
			Map<String, Date> timeRange = CommonConstant.getTimeRange(newsList.getDateRange());
			map.put("start", timeRange.get("start"));
			map.put("end", timeRange.get("end"));
		}
		if (ObjectUtils.isNotEmpty(newsList.getTitle())) {
			map.put("title",newsList.getTitle());
		}

		map.put("num",(page.getCurrent()-1)*page.getSize()+1);
		map.put("size",page.getSize());

		List<NewsList> newsLists = newsListMapper.selectRamdom(map);
		page.setRecords(newsLists);
		return page;
	}

	@Override
	public Integer batchInsert(List<NewsList> newsLists) {
		return newsListMapper.batchNewsInsert(newsLists);
	}
}
